package com.hxzy.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import com.hxzy.common.enums.AckCode;
import com.hxzy.dto.LoginDTO;
import com.hxzy.exception.ServiceException;
import com.hxzy.mapper.LoginMapper;
import com.hxzy.service.LoginService;
import com.hxzy.vo.AccountVO;
import com.hxzy.vo.LoginVO;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 后台登录逻辑实现
 * @author Administrator
 */
@Log4j2
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;

    @Override
    public LoginVO login(LoginDTO loginDTO) {
        //根据用户名查询员工表的信息
        AccountVO accountVO=this.loginMapper.findByLoginName(loginDTO.getLoginName());

        //判断用户在不在
        if(accountVO==null){
            log.error(loginDTO.getLoginName()+",账户不存在");
            throw new ServiceException(AckCode.LOGIN_ACCOUNT_PASSWORD_ERROR);
        }

        //判断密码对不对
        boolean isTrue= BCrypt.checkpw(loginDTO.getLoginPwd(), accountVO.getLoginPwd());
        if(!isTrue){
            log.error(loginDTO.getLoginName()+",密码错误");
            throw new ServiceException(AckCode.LOGIN_ACCOUNT_PASSWORD_ERROR);
        }

        //判断状态是不是禁用
        if(accountVO.getStatus().intValue()==1){
            log.error(loginDTO.getLoginName()+",账户被禁用");
            throw new ServiceException(AckCode.DEVICE_BANNED);
        }

        //生成自定义对象
        LoginVO loginVO=new LoginVO();
        loginVO.setAccountVO(accountVO);

        return loginVO;
    }
}
